package com.aluvfy.xml.test;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import javax.swing.*;
import java.io.InputStream;
import java.util.List;

/**
 * @author: aluvfy
 * @description:
 * @date: 2024/11/19 21:09
 */
public class ParseXMLByDom4j {

    @Test
    public void testParseSqlMapper() throws Exception{
        SAXReader reader = new SAXReader();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("CarMapper.xml");
        Document document = reader.read(is);
        String xpath = "/mapper";
        Element mapper= (Element) document.selectSingleNode(xpath);
        String namespace = mapper.attributeValue("namespace");
        System.out.println("namespace："+ namespace);
        //返回mapper下的所有节点
        List<Element> elements = mapper.elements();
        elements.forEach(element -> {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            System.out.println("id："+ id  + "  resultType："+ resultType );
            String sql = element.getTextTrim();
            System.out.println("sql："+ sql);
            String newSql = sql.replaceAll("#\\{[0-9A-Za-z_$]*}", "?");
            System.out.println("newSql："+ newSql);
        });

    }

    @Test
    public void testParseMybatisConfigXML() throws  Exception{
        //创建SAXReader对象
        SAXReader reader = new SAXReader();
        //获取输入流
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-config.xml");
        //读取xml文件，返回document对象
        Document document = reader.read(is);
        //读取文档中的根标签
//        Element rootElt = document.getRootElement();
//        String rootEltName = rootElt.getName();
//        System.out.println("根标签名称："+rootEltName);

        //获取default的id
        //xpath是做根路径匹配的，能快速定位XML文件中的元素
        String xpath = "/configuration/environments";
        Element environments = (Element) document.selectSingleNode(xpath);  //Element是 Node的子类，方法更多，使用更便捷
        String defaultEnvironmentId = environments.attributeValue("default");
        System.out.println("default的id："+ defaultEnvironmentId);

        //获取具体环境environment
        xpath = "/configuration/environments/environment[@id='"+defaultEnvironmentId+"']";
        Element environment = (Element) document.selectSingleNode(xpath);

        //获取environment下的transactionManager节点（element()方法获取子节点）
        Element transactionManager = environment.element("transactionManager");
        String transactionType = transactionManager.attributeValue("type");
        System.out.println("transactionType："+ transactionType);

        //获取dataSource节点
        Element dataSource = environment.element("dataSource");
        String dataSourceType = dataSource.attributeValue("type");
        System.out.println("dataSourceType："+ dataSourceType);

        //获取dataSource下的所有子节点
        List<Element> propertiesElts = dataSource.elements();
        propertiesElts.forEach(propertiesElt -> {
            String name = propertiesElt.attributeValue("name");
            String value = propertiesElt.attributeValue("value");
            System.out.println("name："+ name + "  value："+ value);
        });

        //获取所有mapper标签
        //不想从根标签下开始获取，你想从任意位置开始，获取所有的某个标签，xpath应该这样些
        xpath = "//mapper";
        List<Node> mappers = document.selectNodes(xpath);
        mappers.forEach(mapper -> {
            Element mapperElt = (Element) mapper;
            String resource = mapperElt.attributeValue("resource");
            System.out.println("resource："+ resource);
        });

    }



}
